package com.cqie.controller;

import com.cqie.dto.MessageDto;
import com.cqie.dto.MessageListDto;
import com.cqie.entity.ChatMessage;
import com.cqie.exception.SpecialException;
import com.cqie.filter.MyWebSocketHandler;
import com.cqie.service.IChatMessageService;
import com.cqie.util.ResultTemplate;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 不吃海鲜
 * @since 2024-12-24
 */

@Tag(name = "聊天室Controller", description = "描述：聊天室接口")
@Slf4j
@RestController
@RequestMapping({"/api/v1.0/chatMessage", "/api/v1/chatMessage"})
public class ChatMessageController {

    @Resource
    private MyWebSocketHandler myWebSocketHandler;
    private final IChatMessageService chatMessageService;

    public ChatMessageController(IChatMessageService chatMessageService) {
        this.chatMessageService = chatMessageService;
    }

    @Operation(summary = "发送消息")
    @PostMapping("/send")
    public ResultTemplate<Boolean> sendMessage(@RequestBody MessageDto messageDto) throws SpecialException {
        log.info("发送的消息: {}", messageDto.getContent());
        myWebSocketHandler.broadcast(messageDto.getContent());
        return ResultTemplate.Success(chatMessageService.sendMessage(messageDto));
    }



    @Operation(summary = "根据会话ID获取消息")
    @GetMapping("/getSession")
    public ResultTemplate<List<MessageListDto>> getMessagesBySessionId(Integer sessionId) {
        return ResultTemplate.Success(chatMessageService.getMessagesBySessionId(sessionId));
    }

    @Operation(summary = "获取会话消息列表")
    @Parameters({
        @Parameter(name = "likeQuery", description = "模糊查询参数")
    })
    @GetMapping("/getSessionList")
    public ResultTemplate<List<MessageListDto>> getSessionList(String likeQuery) {
        return ResultTemplate.Success(chatMessageService.getSessionList(likeQuery));
    }

    @Operation(summary = "创建会议")
    @PostMapping("/createSession")
    public ResultTemplate<Integer> createSession(@RequestBody MessageListDto messageListDto) throws SpecialException {
        return ResultTemplate.Success(chatMessageService.createSession(messageListDto));
    }

    @Operation(summary = "患者发送信息")
    @PostMapping("/patientSend")
    public ResultTemplate<Boolean> patientSend(@RequestBody MessageDto messageDto) throws SpecialException {
        log.info("发送的消息: {}", messageDto.getContent());
        myWebSocketHandler.broadcast(messageDto.getContent());
        return ResultTemplate.Success(chatMessageService.sendMessage(messageDto));
    }

    @Operation(summary = "根据SessionId获取消息")
    @GetMapping("/getMessageList")
    public ResultTemplate<List<MessageListDto>> getBySessionId(@RequestParam(required = true) Integer sessionId) throws SpecialException {
        return ResultTemplate.Success(chatMessageService.getBySessionId(sessionId));
    }
//
//    // 发送新消息
//    @PostMapping
//    public Message createMessage(@RequestBody Message message) {
//        return messageService.save(message);
//    }
//
//    // 更新消息
//    @PutMapping("/{id}")
//    public Message updateMessage(@PathVariable Long id, @RequestBody Message message) {
//        message.setId(id);
//        return messageService.updateById(message);
//    }
//
//    // 删除消息
//    @DeleteMapping("/{id}")
//    public boolean deleteMessage(@PathVariable Long id) {
//        return messageService.removeById(id);
//    }
}
